module GYLite
{
									
					
	export class ButtonSkin extends GYSkin implements IButtonSkin
	{
		protected _stsVec:egret.Texture[];
		protected _curSkin:any;
		protected _text:GYText;
		protected _hasLabel:boolean;
		protected _scale9GridRect:Scale9GridRect;
		
		/**按钮皮肤，自定义需实现接口IButtonSkin
		 * 皮肤数组，包括8个状态的Texture
		 * */
		public constructor(skinVec:egret.Texture[],rect:Scale9GridRect=null)
		{
			super();
			var s = this;			
			s._stsVec=skinVec;
			if(s._stsVec.length > 8 || s._stsVec.length == 0)
				throw new Error("按钮皮肤参数不对！#" + s._stsVec.length);
			s._curSkin=new GYScaleSprite;
				s._scale9GridRect=s._curSkin.scale9GridRect=rect;			
			s._curSkin.texture = s._stsVec[0];
		}
		public release():void
		{var s = this;
			s._curSkin.texture = null;
		}
		public drawSkin(state:number):void
		{var s = this;
			s._curSkin.texture=(state<s._stsVec.length && s._stsVec[state])? s._stsVec[state] : s._stsVec[0];
		}
		public set hostComponent(val:GYSprite)
		{var s = this;
			if(val==s._hostComponent)
				return;
			if(s._hostComponent)
			{
				if(s._curSkin.parent)
					s._hostComponent.removeElement(s._curSkin);
				if(s._hasLabel)
					s._hostComponent.removeElement(s._text);
			}
			s._hostComponent=val;
			if(s._hostComponent)
			{
				s._hostComponent.addElementAt(s._curSkin, 0);
				if(!isNaN(s._hostComponent.settingWidth))
				{					
					s._curSkin.width = s._hostComponent.width;
				}
				if(!isNaN(s._hostComponent.settingHeight))
				{					
					s._curSkin.height = s._hostComponent.height;				
				}				
				if(s._hasLabel)
				{
					s._hostComponent.addElement(s._text);
					s._text.width=isNaN(s._hostComponent.settingHeight)?s._stsVec[0].textureWidth:s._hostComponent.width;
				}
			}
		}
		public get hostComponent():GYSprite
		{var s = this;
			return s._hostComponent;
		}
		public get width():number
		{
			return this._curSkin.width;
		}
		public get height():number
		{
			return this._curSkin.height;
		}
		public set width(value:number) 
		{var s = this;
			if(s._hasLabel)
				s._text.width = value;
			s._curSkin.width = value==value?value:s._stsVec[0].textureWidth;
		}		
		public set height(value:number) 
		{var s = this;
			s._curSkin.height = value==value?value:s._stsVec[0].textureHeight;
		}		
		public set scale9GridRect(value:Scale9GridRect) 
		{var s = this;
			s._scale9GridRect=value;
			s._curSkin.scale9GridRect = value;			
		}
		public get scale9GridRect():Scale9GridRect
		{
			return this._scale9GridRect;
		}
		
		/**当s.label被赋值的时候自动产生Mytext文本(在此之前不存在s.labelDisplay)，在s.label为null时，文本对象不会被清除* */
		public set label(val:string)
		{var s = this;
			if(val == null)
			{
				if(s._hasLabel)
				{
					s._hasLabel=false;
					if(s._hostComponent)
						s._hostComponent.removeElement(s._text);
				}
				return;
			}
			
			if(s._text==null)
			{
				s._text=new GYText;				
				s._text.align="center";
				// s._text.selectable=false;
//				s._text.color = 0x555555;
				s._text.verticalCenter = 0;
				if(s._hostComponent)
				{
					s._hostComponent.addElement(s._text);
					s._text.width=isNaN(s._hostComponent.settingWidth)?s._stsVec[0].textureWidth:s._hostComponent.width;
				}
			}
			s._hasLabel=true;
			s._text.text=val;
		}
		public get label():string
		{var s = this;
			if(s._hasLabel)
				return s._text.text;
			return "";
		}
		public get labelDisplay():GYText
		{var s = this;
			if(s._hasLabel)
				return s._text;
			return null;
		}
		public set labelDisplay(val:GYText)
		{var s = this;
			if(s._text && s._text.parent)
				s._hostComponent.removeElement(s._text);
			s._text = val;
			if(s._text)
			{
				s._hasLabel = true;
				if(s._hostComponent)
				{
					s._hostComponent.addElement(s._text);
					s._text.width=s._hostComponent.width;
					s._text.verticalCenter = 0;
				}
			}
		}
		/**此函数只复制s.labelDisplay的textFormat部分属性 和复制布局*/
		public cloneLabel():GYText
		{var s = this;
			if(s._hasLabel)
			{
				var newText:GYText=new GYText;
				var newFormat:TextFormat = new TextFormat;
				var f:TextFormat = s._text.textFormat;
				newFormat.align = f.align;
				newFormat.bold = f.bold;
				newFormat.color = f.color;
				newFormat.size = f.size;
				newFormat.font = f.font;
				newFormat.italic = f.italic;
				newFormat.underline = f.underline;
				newFormat.leading = f.leading;
				newText.textFormat = newFormat;
				newText.layoutMode = s._text.layoutMode.clone();
				newText.x = s._text.x;
				newText.y = s._text.y;
				newText.text = s._text.text;
				return newText;
			}
			return null;
		}
		/**此函数复制皮肤属性 如s.label等除按钮本身的其他属性（目前只有Label）*/
		public copy(skin:IGYSkin)
		{
			if(skin == null)
				return;
			var s = this;
			s.labelDisplay = (skin as IButtonSkin).cloneLabel();
		}
		/**此函数不克隆s.labelDisplay*/
		public clone():IGYSkin
		{var s = this;
			var buttonSkin:ButtonSkin=new ButtonSkin(s._stsVec,s._scale9GridRect);
			return buttonSkin;
		}
	}
}